package puffer.starter.db;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import puffer.starter.db.dao.Dao;
import puffer.starter.db.entity.Dnt;
import puffer.starter.db.entity.Page;
import puffer.starter.db.strengthen.Wrapper;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 增强Dao实现类
 *
 * @author 王永吉
 */
@RestController
@RequestMapping("/")
public class TestController {
    @Autowired
    Dao pfDao2;

    @RequestMapping("/insertOrigin")
    public long insertOrigin() {
        return pfDao2.insertOrigin("insert into a_template (name,d_ct,d_ut) values (1,'2021-07-21 08:42:08','2021-07-21 01:42:08')");
    }

    @RequestMapping("/insert")
    public long insert() {
        Dnt ent = new Dnt();
        ent.put("name", 50);
        ent.put("d_ct", new Date());
        ent.put("d_ut", LocalDateTime.now());
        return pfDao2.insert("a_template", ent);
    }

    @RequestMapping("/insertBatch1")
    public long insertBatch1() {
        Dnt ent1 = new Dnt();
        ent1.put("name", 81);
        ent1.put("d_ct", new Date());
        ent1.put("d_ut", LocalDateTime.now());
        Dnt ent2 = new Dnt();
        ent2.put("name", 82);
        ent2.put("d_ct", new Date());
        ent2.put("d_ut", LocalDateTime.now());
        Dnt ent3 = new Dnt();
        ent3.put("name", 83);
        ent3.put("d_ct", new Date());
        ent3.put("d_ut", LocalDateTime.now());
        List<Dnt> ents = Arrays.asList(ent1, ent2, ent3);
        final long l = pfDao2.insertBatch("a_template", ents);
        System.out.println(JSON.toJSONString(ents));
        return l;
    }

    //    public <A> List<A> asList(A... a) {
    //        return null;
    //    }

    @RequestMapping("/insertBatch2")
    public long insertBatch2() {
        Dnt ent1 = new Dnt();
        ent1.put("name", 81);
        ent1.put("d_ct", new Date());
        ent1.put("d_ut", LocalDateTime.now());
        Dnt ent2 = new Dnt();
        ent2.put("name", 82);
        ent2.put("d_ct", new Date());
        ent2.put("d_ut", LocalDateTime.now());
        Dnt ent3 = new Dnt();
        ent3.put("name", 83);
        ent3.put("d_ct", new Date());
        ent3.put("d_ut", LocalDateTime.now());
        List<Dnt> ents = Arrays.asList(ent1, ent2, ent3);
        final long l = pfDao2.insertBatch("a_template", ents, 1);
        System.out.println(JSON.toJSONString(ents));
        return l;
    }

    @RequestMapping("/deleteOrigin")
    public long deleteOrigin() {
        pfDao2.insertOrigin("insert into a_template (id,name) values (10001,10001)");
        pfDao2.insertOrigin("insert into a_template (id,name) values (10002,10002)");
        pfDao2.insertOrigin("insert into a_template (id,name) values (10003,10003)");

        return pfDao2.deleteOrigin("delete from a_template where 1=1");
    }

    @RequestMapping("/delete")
    public long delete() {
        pfDao2.insertOrigin("insert into a_template (id,name) values (10001,10001)");
        pfDao2.insertOrigin("insert into a_template (id,name) values (10002,10002)");
        pfDao2.insertOrigin("insert into a_template (id,name) values (10003,10003)");

        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.ne("id", "0");
        return pfDao2.delete("a_template", wrapper);

    }

    @RequestMapping("/deleteById1")
    public long deleteById1() {
        pfDao2.insertOrigin("insert into a_template (id,name) values (10001,10001)");
        pfDao2.insertOrigin("insert into a_template (id,name) values (10002,10002)");
        pfDao2.insertOrigin("insert into a_template (id,name) values (10003,10003)");
        pfDao2.deleteById("a_template", 10001);
        pfDao2.deleteById("a_template", 10002);
        pfDao2.deleteById("a_template", 10003);
        return 0;
    }
//
//    @RequestMapping("/deleteById2")
//    public long deleteById2() {
//        pfDao2.insertOrigin("insert into a_template (id,name) values (10001,10001)");
//        pfDao2.insertOrigin("insert into a_template (id,name) values (10002,10002)");
//        pfDao2.insertOrigin("insert into a_template (id,name) values (10003,10003)");
//        pfDao2.deleteById("a_template", "id", 10001);
//        pfDao2.deleteById("a_template", "id", 10002);
//        pfDao2.deleteById("a_template", "id", 10003);
//        return 0;
//    }

    @RequestMapping("/deleteByIds1")
    public long deleteByIds1() {
        pfDao2.insertOrigin("insert into a_template (id,name) values (10001,10001)");
        pfDao2.insertOrigin("insert into a_template (id,name) values (10002,10002)");
        pfDao2.insertOrigin("insert into a_template (id,name) values (10003,10003)");
        long l = pfDao2.deleteByIds("a_template", Arrays.asList(10001, 10002, 10003));
        return l;
    }

    @RequestMapping("/deleteByIds2")
    public long deleteByIds2() {
        pfDao2.insertOrigin("insert into a_template (id,name) values (10001,10001)");
        pfDao2.insertOrigin("insert into a_template (id,name) values (10002,10002)");
        pfDao2.insertOrigin("insert into a_template (id,name) values (10003,10003)");
        long l = pfDao2.deleteByIds("a_template", Arrays.asList(10001, 10002, 10003), 1);
        return l;
    }

//    @RequestMapping("/deleteByIds3")
//    public long deleteByIds3() {
//        pfDao2.insertOrigin("insert into a_template (id,name) values (10001,10001)");
//        pfDao2.insertOrigin("insert into a_template (id,name) values (10002,10002)");
//        pfDao2.insertOrigin("insert into a_template (id,name) values (10003,10003)");
//        long l = pfDao2.deleteByIds("a_template", "id", Arrays.asList(10001, 10002, 10003));
//        return l;
//    }

//    @RequestMapping("/deleteByIds4")
//    public long deleteByIds4() {
//        pfDao2.insertOrigin("insert into a_template (id,name) values (10001,10001)");
//        pfDao2.insertOrigin("insert into a_template (id,name) values (10002,10002)");
//        pfDao2.insertOrigin("insert into a_template (id,name) values (10003,10003)");
//        long l = pfDao2.deleteByIds("a_template", "id", Arrays.asList(10001, 10002, 10003), 1);
//        return l;
//    }

    @RequestMapping("/updateOrigin")
    public long updateOrigin() {
        pfDao2.insertOrigin("insert into a_template (name,d_ct,d_ut) values (10001,'2021-01-01 01:01:01','2021-01-01 01:01:01')");
        pfDao2.insertOrigin("insert into a_template (name,d_ct,d_ut) values (10002,'2021-01-01 01:01:01','2021-01-01 01:01:01')");
        pfDao2.insertOrigin("insert into a_template (name,d_ct,d_ut) values (10003,'2021-01-01 01:01:01','2021-01-01 01:01:01')");
        pfDao2.updateOrigin("update a_template set name='updateOriginppppppppppppppp',d_ct='2022-01-01 01:01:01',d_ut='2022-01-01 01:01:01'  where id != 0 ");
        return 0;
    }

    @RequestMapping("/update1")
    public long update1() {
        pfDao2.insertOrigin("insert into a_template (name,d_ct,d_ut) values (10001,'2021-01-01 01:01:01','2021-01-01 01:01:01')");
        pfDao2.insertOrigin("insert into a_template (name,d_ct,d_ut) values (10002,'2021-01-01 01:01:01','2021-01-01 01:01:01')");
        pfDao2.insertOrigin("insert into a_template (name,d_ct,d_ut) values (10003,'2021-01-01 01:01:01','2021-01-01 01:01:01')");
        pfDao2.updateOrigin("update a_template set name='updateOriginppppppppppppppp',d_ct='2022-01-01 01:01:01',d_ut='2022-01-01 01:01:01'  where id != 0 ");

        Dnt ent = new Dnt();
        ent.put("name", "update1-ppppppppppppppp");
        ent.put("d_ct", new Date());
        ent.put("d_ut", LocalDateTime.now());
        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.ne("id", 0);
        pfDao2.update("a_template", ent, wrapper);
        return 0;
    }

    @RequestMapping("/update2")
    public long update2() {
        pfDao2.insertOrigin("insert into a_template (name) values (10001)");
        pfDao2.insertOrigin("insert into a_template (name) values (10002)");
        pfDao2.insertOrigin("insert into a_template (name) values (10003)");

        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.ne("id", 0);
        wrapper.set("name", "update2-QQQQQQ");
        wrapper.set("d_ct", new Date());
        wrapper.set("d_ut", LocalDateTime.now());

        pfDao2.update("a_template", wrapper);
        return 0;
    }

    @RequestMapping("/updateById1")
    public long updateById1() {
        pfDao2.insertOrigin("insert into a_template (name) values (10001)");
        pfDao2.insertOrigin("insert into a_template (name) values (10002)");
        pfDao2.insertOrigin("insert into a_template (name) values (10003)");

        Dnt ent = new Dnt();
        ent.put("name", "updateById1-ppppppppppppppp");
        ent.put("d_ct", new Date());
        ent.put("d_ut", LocalDateTime.now());
        pfDao2.updateById("a_template", ent);
        return 0;
    }
//
//    @RequestMapping("/updateById2")
//    public long updateById2() {
//        pfDao2.insertOrigin("insert into a_template (name) values (10001)");
//        pfDao2.insertOrigin("insert into a_template (name) values (10002)");
//        pfDao2.insertOrigin("insert into a_template (name) values (10003)");
//
//        Ent ent = new Ent();
//        ent.put("name", "updateById2-MMMMMMMMMMMMMM");
//        pfDao2.updateById("a_template", "id", ent, 1020);
//        return 0;
//    }

    @RequestMapping("/updateBatchById1")
    public long updateBatchById1() {
        pfDao2.insertOrigin("insert into a_template (id) values (10001)");
        pfDao2.insertOrigin("insert into a_template (id) values (10002)");
        pfDao2.insertOrigin("insert into a_template (id) values (10003)");

        Dnt ent1 = new Dnt();
        ent1.put("id", "10001");
        ent1.put("name", "1001-MMMMMMMMMMMMMM");
        ent1.put("d_ct", new Date());
        ent1.put("d_ut", LocalDateTime.now());

        Dnt ent2 = new Dnt();
        ent2.put("id", "10002");
        ent2.put("name", "1002-MMMMMMMMMMMMMM");
        ent2.put("d_ct", new Date());
        ent2.put("d_ut", LocalDateTime.now());

        Dnt ent3 = new Dnt();
        ent3.put("id", "1003");
        ent3.put("name", "1003-MMMMMMMMMMMMMM");
        ent3.put("d_ct", new Date());
        ent3.put("d_ut", LocalDateTime.now());

        List<Dnt> ents = Arrays.asList(ent1, ent2, ent3);
        pfDao2.updateBatchById("a_template", ents);
        return 0;
    }

    @RequestMapping("/updateBatchById2")
    public long updateBatchById2() {
        pfDao2.insertOrigin("insert into a_template (id) values (10001)");
        pfDao2.insertOrigin("insert into a_template (id) values (10002)");
        pfDao2.insertOrigin("insert into a_template (id) values (10003)");

        Dnt ent1 = new Dnt();
        ent1.put("id", "1001");
        ent1.put("name", "1001-MMMMMMMMMMMMMM");
        ent1.put("d_ct", new Date());
        ent1.put("d_ut", LocalDateTime.now());
        Dnt ent2 = new Dnt();
        ent2.put("id", "10002");
        ent2.put("name", "1002-MMMMMMMMMMMMMM");
        ent2.put("d_ct", new Date());
        ent2.put("d_ut", LocalDateTime.now());
        Dnt ent3 = new Dnt();
        ent3.put("id", "10003");
        ent3.put("name", "1003-MMMMMMMMMMMMMM");
        ent3.put("d_ct", new Date());
        ent3.put("d_ut", LocalDateTime.now());
        List<Dnt> ents = Arrays.asList(ent1, ent2, ent3);
        pfDao2.updateBatchById("a_template", ents, 1);
        return 0;
    }

//    @RequestMapping("/updateBatchById3")
//    public long updateBatchById3() {
//        pfDao2.insertOrigin("insert into a_template (name) values (10001)");
//        pfDao2.insertOrigin("insert into a_template (name) values (10002)");
//        pfDao2.insertOrigin("insert into a_template (name) values (10003)");
//
//        Ent ent1 = new Ent();
//        ent1.put("id", "1001");
//        ent1.put("name", "1001-MMMMMMMMMMMMMM");
//        Ent ent2 = new Ent();
//        ent2.put("id", "1002");
//        ent2.put("name", "1002-MMMMMMMMMMMMMM");
//        Ent ent3 = new Ent();
//        ent3.put("id", "1003");
//        ent3.put("name", "1003-MMMMMMMMMMMMMM");
//        List<Ent> ents = Arrays.asList(ent1, ent2, ent3);
//        pfDao2.updateBatchById("a_template", "id", ents);
//        return 0;
//    }
//
//    @RequestMapping("/updateBatchById4")
//    public long updateBatchById4() {
//        pfDao2.insertOrigin("insert into a_template (name) values (10001)");
//        pfDao2.insertOrigin("insert into a_template (name) values (10002)");
//        pfDao2.insertOrigin("insert into a_template (name) values (10003)");
//
//        Ent ent1 = new Ent();
//        ent1.put("id", "1001");
//        ent1.put("name", "1001-MMMMMMMMMMMMMM");
//        Ent ent2 = new Ent();
//        ent2.put("id", "1002");
//        ent2.put("name", "1002-MMMMMMMMMMMMMM");
//        Ent ent3 = new Ent();
//        ent3.put("id", "1003");
//        ent3.put("name", "1003-MMMMMMMMMMMMMM");
//        List<Ent> ents = Arrays.asList(ent1, ent2, ent3);
//        pfDao2.updateBatchById("a_template", "id", ents, 1);
//        return 0;
//    }

    @RequestMapping("/selectOne")
    public long selectOne() {
        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.select("*");
        wrapper.ne("id", 0);

        //解析时输出null
        JSON.DEFAULT_GENERATE_FEATURE = SerializerFeature.WriteMapNullValue.getMask();
        //
        //JSON.DEFAULT_PARSER_FEATURE = SerializerFeature.WriteMapNullValue.getMask();
        final Dnt ent = pfDao2.selectOne("a_template", wrapper);
        final String s = JSONObject.toJSONString(ent);
        System.out.println();
        return 0;
    }

    @RequestMapping("/selectOneOrigin")
    public long selectOneOrigin() {
        final Dnt ent = pfDao2.selectOneOrigin("select * from a_template where id!=0");
        System.out.println(JSON.toJSONString(ent));
        return 0;
    }

    @RequestMapping("/selectObj")
    public long selectObj() {
        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.select("name");
        wrapper.ne("id", 0);
        final Object o = pfDao2.selectObj("a_template", wrapper);
        System.out.println(JSON.toJSONString(o));
        return 0;
    }

    @RequestMapping("/selectObjOrigin")
    public long selectObjOrigin() {
        final Object o = pfDao2.selectObjOrigin("select name from a_template where id!=0");
        System.out.println(JSON.toJSONString(o));
        return 0;
    }

    @RequestMapping("/list")
    public long list() {
        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.select("name");
        wrapper.ne("id", 0);
        final List<Dnt> list = pfDao2.list("a_template", wrapper);
        System.out.println(JSON.toJSONString(list));
        return 0;
    }

    @RequestMapping("/listOrigin")
    public long listOrigin() {
        final List<Dnt> ents = pfDao2.listOrigin("select name from a_template where id!=0");
        System.out.println(JSON.toJSONString(ents));
        return 0;
    }

    @RequestMapping("/listObj")
    public long listObj() {
        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.select("name");
        wrapper.ne("id", 0);
        final List<Object> objects = pfDao2.listObj("a_template", wrapper);
        System.out.println(JSON.toJSONString(objects));
        return 0;
    }

    @RequestMapping("/listObjOrigin")
    public long listObjOrigin() {
        Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.select("name");
        wrapper.ne("id", 0);
        final List<Object> objects = pfDao2.listObjOrigin("select name from a_template where id!=0");
        System.out.println(JSON.toJSONString(objects));
        return 0;
    }

    @RequestMapping("/count")
    public long count() {
        final Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.select("count(1)");
        wrapper.ne("id", 0);
        long count = pfDao2.count("a_template", wrapper);

        return count;
    }

    @RequestMapping("/countOrigin")
    public long countOrigin() {
        final long l = pfDao2.countOrigin("select count(1) from a_template where id!=0");
        return l;
    }

    @RequestMapping("/page1")
    public Page<Dnt> page1() {
        return pfDao2.page("a_template", new Page<>(1, 10));
    }

    @RequestMapping("/page2")
    public Page<Dnt> page2() {
        final Wrapper<Object> wrapper = new Wrapper<>();
        wrapper.select("name");
        wrapper.ne("id", 0);
        return pfDao2.page("a_template", wrapper, new Page<>(1, 10));
    }

    @RequestMapping("/page3")
    public Page<Dnt> page3() {
        return pfDao2.pageOrigin("select name from a_template where id!=0", new Page<>(1, 10));
    }

}
